﻿#include "setup2wind.h"
#include "ui_setup2wind.h"
extern intListener doorStatus;
QSize CustomTabStyle4::sizeFromContents(ContentsType type, const QStyleOption *option, const QSize &size, const QWidget *widget) const
{
    QSize s=QProxyStyle::sizeFromContents(type,option,size,widget);
    if(type==QStyle::CT_TabBarTab){
        s.transposed();
        s.rwidth()=168;
        s.rheight()=50;
    }
    return s;
}
void CustomTabStyle4::drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
    if(element==CE_TabBarTabLabel){
        if(const QStyleOptionTab *tab=qstyleoption_cast<const QStyleOptionTab *>(option)){
            QRect allRect=tab->rect;
            if(tab->state&QStyle::State_Selected){
                painter->save();
                painter->setPen(0x89cfff);
                painter->setBrush(QBrush(0x89cfff));
                painter->drawRect(allRect.adjusted(6,6,-6,-6));
                painter->restore();
            }
            QTextOption option;
            option.setAlignment(Qt::AlignCenter);
            if(tab->state&QStyle::State_Selected){
                painter->setPen(0xf8fcff);
            }else{
                painter->setPen(0x000001);
            }
            painter->drawText(allRect,tab->text,option);
            return ;
        }
    }
    if(element==CE_TabBarTab){
        QProxyStyle::drawControl(element,option,painter,widget);
    }
}
class ReadOnlyDelegate: public QItemDelegate
{

public:
    ReadOnlyDelegate(QWidget *parent = NULL):QItemDelegate(parent)
    {}

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                          const QModelIndex &index) const override //final
    {
        Q_UNUSED(parent)
        Q_UNUSED(option)
        Q_UNUSED(index)
        return NULL;
    }
};

setup2wind::setup2wind(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::setup2wind)
{
    ui->setupUi(this);
    connect(&doorStatus, &intListener::valueChanged,this,&setup2wind::doorStatusChanged,Qt::QueuedConnection);
    setWindowIcon(QIcon(":/pic/picture/kafavicon.ico"));
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40);
    ui->tableWidget->setInputMethodHints(Qt::ImhHiddenText);// 表格单击进入
    for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
        ui->tableWidget->setRowHeight(i, 50);
    }
    ui->tableWidget_2->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget_2->verticalHeader()->setDefaultSectionSize(40);
    ui->tableWidget_2->setInputMethodHints(Qt::ImhHiddenText);// 表格单击进入
    for (int i = 0; i < ui->tableWidget_2->rowCount(); ++i) {
        ui->tableWidget_2->setRowHeight(i, 50);
    }
    ui->tableWidget_3->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget_3->verticalHeader()->setDefaultSectionSize(40);
    ui->tableWidget_3->setInputMethodHints(Qt::ImhHiddenText);// 表格单击进入
    for (int i = 0; i < ui->tableWidget_3->rowCount(); ++i) {
        ui->tableWidget_3->setRowHeight(i, 50);
    }
    ui->tabWidget->tabBar()->setStyle(new CustomTabStyle4);
#ifdef Q_OS_ANDROID
    int fontId = QFontDatabase::addApplicationFont(":/FontStyle/AlibabaPuHuiTi-3-55-Regular.ttf");
    if (fontId != -1) {
        QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId);
        if (!fontFamilies.isEmpty()) {
            QString familyName = fontFamilies.first();
            QFont font(familyName, 18); // 创建一个QFont对象，设置字体名称和默认大小

            // 使用setFont方法设置字体，传入QFont对象
            ui->comboBox->setFont(font);
            ui->comboBox_2->setFont(font);
            ui->comboBox_3->setFont(font);
            ui->tableWidget->setFont(font);
            ui->tableWidget_2->setFont(font);
            ui->tableWidget_3->setFont(font);
            ui->widget_3->setFont(font);


            // 如果需要为其他按钮设置不同的大小，可以创建新的QFont对象
            QFont smallerFont(familyName, 16);
            ui->returnBtn->setFont(familyName);
            ui->pushButton_9->setFont(familyName);
        }
    }
#endif

}

void setup2wind::doorStatusChanged(){
    if(doorStatus.get()==0)
    {
        ui->pushButton_9->setText("        打开仓门");
    }
    else
    {
        ui->pushButton_9->setText("        关闭仓门");
    }
}

setup2wind::~setup2wind()
{
    delete ui;
}

void setup2wind::showEvent(QShowEvent *event)
{
    tcpClient = TcpClient::getInstance();
    if(doorStatus.get()==0)
    {
        ui->pushButton_9->setText("        打开仓门");
    }
    else
    {
        ui->pushButton_9->setText("        关闭仓门");
    }
    refTableData();
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40);
    for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
        ui->tableWidget->setRowHeight(i, 50);
    }
    refTableData();
    ui->tableWidget_2->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget_2->verticalHeader()->setDefaultSectionSize(40);
    for (int i = 0; i < ui->tableWidget_2->rowCount(); ++i) {
        ui->tableWidget_2->setRowHeight(i, 50);
    }
    refTableData();
    ui->tableWidget_3->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget_3->verticalHeader()->setDefaultSectionSize(40);
    for (int i = 0; i < ui->tableWidget_3->rowCount(); ++i) {
        ui->tableWidget_3->setRowHeight(i, 50);
    }
    event->accept();
}

void setup2wind::on_SaveBtn_clicked()
{
    this->close();
    emit changeTotalWindow();
}

void setup2wind::on_returnBtn_clicked()
{
    this->close();
    emit changeTotalWindow();
}

void setup2wind::on_pushButton_9_clicked()
{
    if(doorStatus.get()==0)
    {
        doorStatus.set(1);
        ui->pushButton_9->setText("        关闭仓门");
    }
    else
    {
        doorStatus.set(0);
        ui->pushButton_9->setText("        打开仓门");
    }
}

void setup2wind::on_comboBox_currentTextChanged(const QString &arg1)
{
    refTableData();
    if(arg1=="扩增体系(μl)"){
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="DELETE FROM ItemCode WHERE Name=''and DicCode = '4'";
        query.exec(sql);
    }
    refTableData();
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40);
    for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
        ui->tableWidget->setRowHeight(i, 50);
    }
}

void setup2wind::on_comboBox_2_currentTextChanged(const QString &arg1)
{
    refTableData();
    ui->tableWidget_2->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget_2->verticalHeader()->setDefaultSectionSize(40);
    for (int i = 0; i < ui->tableWidget_2->rowCount(); ++i) {
        ui->tableWidget_2->setRowHeight(i, 50);
    }
}

void setup2wind::on_comboBox_3_currentTextChanged(const QString &arg1)
{
    refTableData();
    ui->tableWidget_3->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget_3->verticalHeader()->setDefaultSectionSize(40);
    for (int i = 0; i < ui->tableWidget_3->rowCount(); ++i) {
        ui->tableWidget_3->setRowHeight(i, 50);
    }
}

// 刷新
void setup2wind::refTableData(){
    ui->tableWidget->blockSignals(true);
    ui->tableWidget_2->blockSignals(true);
    ui->tableWidget_3->blockSignals(true);
    QString dicCode;
    if(ui->tabWidget->currentIndex()==0){
        if(ui->comboBox->currentText()=="样本名称"){
            dicCode = "1";
        }else if(ui->comboBox->currentText()=="样本类型"){
            dicCode = "2";
        }else if(ui->comboBox->currentText()=="扩增体系(μl)"){
            dicCode = "4";
        }else if(ui->comboBox->currentText()=="耗材类型"){
            dicCode = "6";
        }else if(ui->comboBox->currentText()=="试剂盒"){
            dicCode = "12";
        }else if(ui->comboBox->currentText()=="热盖温度(℃)"){
            dicCode = "20";
        }
    }else if(ui->tabWidget->currentIndex()==1){
        if(ui->comboBox_2->currentText()=="通道一"){
            dicCode = "7";
        }else if(ui->comboBox_2->currentText()=="通道二"){
            dicCode = "8";
        }else if(ui->comboBox_2->currentText()=="通道三"){
            dicCode = "9";
        }else if(ui->comboBox_2->currentText()=="通道四"){
            dicCode = "10";
        }else if(ui->comboBox_2->currentText()=="内标"){
            dicCode = "11";
        }
    }else if(ui->tabWidget->currentIndex()==2){
        if(ui->comboBox_3->currentText()=="基因分析仪"){
            dicCode = "14";
        }else if(ui->comboBox_3->currentText()=="试剂盒类型"){
            dicCode = "15";
        }else if(ui->comboBox_3->currentText()=="体系(μl)"){
            dicCode = "19";
        }else{
            QString info=ui->comboBox_3->currentText();
            QString type=  info.left(info.length()-3);
            QSqlDatabase db = dbHelper::openConnection();
            QSqlQuery query(db);

            QString sql=QString("Select Code From ItemCode Where Name='%1'").arg(type);
            query.exec(sql);
            while (query.next()) {
                dicCode=query.value("Code").toString();
            }
        }
    }
    if(dicCode=="4"){
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql=QString("delete  FROM ItemCode WHERE DicCode='%1' and Name=" " ").arg(dicCode);
        query.exec(sql);
    }
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql=QString("SELECT Code,Name,IsDefault FROM ItemCode WHERE DicCode='%1'").arg(dicCode);
    QStringList codes,sanames,isds;
    query.exec(sql);
    while (query.next()) {
        QString column1Value=query.value("Code").toString();
        QString column2Value = query.value("Name").toString();
        QString column3Value = query.value("IsDefault").toString();
        codes.append(column1Value);
        sanames.append(column2Value);
        isds.append(column3Value);
    }
    if(ui->tabWidget->currentIndex()==0){
        ui->tableWidget->setRowCount(sanames.length());
        ui->tableWidget->setColumnCount(3);
        ui->tableWidget->setWindowTitle("QTableWidget & Item");
        ui->tableWidget->resizeColumnsToContents();
        ui->tableWidget->resizeRowsToContents();

        QStringList header;
        header.append("");
        header.append("信息");
        header.append("操作");
        ui->tableWidget->setHorizontalHeaderLabels(header);
        ui->tableWidget->verticalHeader()->setVisible(false);
        ui->tableWidget->setColumnHidden(0, true); // 隐藏第1列
    }else if(ui->tabWidget->currentIndex()==1){
        ui->tableWidget_2->setRowCount(sanames.length());
        ui->tableWidget_2->setColumnCount(3);
        ui->tableWidget_2->setWindowTitle("QTableWidget & Item");
        ui->tableWidget_2->resizeColumnsToContents();
        ui->tableWidget_2->resizeRowsToContents();

        QStringList header;
        header.append("");
        header.append("信息");
        header.append("操作");
        ui->tableWidget_2->setHorizontalHeaderLabels(header);
        ui->tableWidget_2->verticalHeader()->setVisible(false);
        ui->tableWidget_2->setColumnHidden(0, true); // 隐藏第1列
    }else if(ui->tabWidget->currentIndex()==2){
        ui->tableWidget_3->setRowCount(sanames.length());
        ui->tableWidget_3->setColumnCount(3);
        ui->tableWidget_3->setWindowTitle("QTableWidget & Item");
        ui->tableWidget_3->resizeColumnsToContents();
        ui->tableWidget_3->resizeRowsToContents();

        QStringList header;
        header.append("");
        header.append("信息");
        header.append("操作");
        ui->tableWidget_3->setHorizontalHeaderLabels(header);
        ui->tableWidget_3->verticalHeader()->setVisible(false);
        ui->tableWidget_3->setColumnHidden(0, true); // 隐藏第1列
    }


    for(int i=0;i<sanames.length();i++){
        QPushButton *btn_1 = new QPushButton();
        QPushButton *btn_2 = new QPushButton();
        QPushButton *btn_3 = new QPushButton();
        btn_1->setText(tr("添加"));
        btn_2->setText(tr("默认"));
        btn_3->setText(tr("删除"));
        if(isds[i]=="1"){
            btn_2->setStyleSheet("background-color: rgb(173, 216, 230);");
        }
        btn_1->setMinimumSize(60,30);
        btn_2->setMinimumSize(60,30);
        btn_3->setMinimumSize(60,30);
        QWidget *tmp_widget = new QWidget();
        QHBoxLayout *tmp_layout = new QHBoxLayout(tmp_widget);
        if(dicCode == "11"){
            tmp_layout->addWidget(btn_2);
        }else if(dicCode == "20"){
            tmp_layout->addWidget(btn_2);
        }else{
            tmp_layout->addWidget(btn_1);
            tmp_layout->addWidget(btn_2);
            tmp_layout->addWidget(btn_3);
        }
        if(ui->tabWidget->currentIndex()==0){
            ui->tableWidget->setItem(i,0,new QTableWidgetItem(codes[i]));
            ui->tableWidget->setItem(i,1,new QTableWidgetItem(sanames[i]));
            ui->tableWidget->setCellWidget(i,2,tmp_widget);
        }else if(ui->tabWidget->currentIndex()==1){
            ui->tableWidget_2->setItem(i,0,new QTableWidgetItem(codes[i]));
            ui->tableWidget_2->setItem(i,1,new QTableWidgetItem(sanames[i]));
            ui->tableWidget_2->setCellWidget(i,2,tmp_widget);
        }else if(ui->tabWidget->currentIndex()==2){
            ui->tableWidget_3->setItem(i,0,new QTableWidgetItem(codes[i]));
            ui->tableWidget_3->setItem(i,1,new QTableWidgetItem(sanames[i]));
            ui->tableWidget_3->setCellWidget(i,2,tmp_widget);
        }
#ifdef Q_OS_ANDROID
    int fontId = QFontDatabase::addApplicationFont(":/FontStyle/AlibabaPuHuiTi-3-55-Regular.ttf");
    if (fontId != -1) {
        QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId);
        if (!fontFamilies.isEmpty()) {
            QString familyName = fontFamilies.first();
            QFont font(familyName, 18); // 创建一个QFont对象，设置字体名称和默认大小
            btn_1->setFont(font);
            btn_2->setFont(font);
            btn_3->setFont(font);

        }
    }
#endif

        connect(btn_1,SIGNAL(clicked()),this,SLOT(add_clicked()));
        connect(btn_2,SIGNAL(clicked()),this,SLOT(set_clicked()));
        connect(btn_3,SIGNAL(clicked()),this,SLOT(del_clicked()));
    }
    // 表头颜色
    ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}");
    ui->tableWidget->show();
    ui->tableWidget->blockSignals(false);
    ui->tableWidget_2->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}");
    ui->tableWidget_2->show();
    ui->tableWidget_2->blockSignals(false);
    ui->tableWidget_3->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}");
    ui->tableWidget_3->show();
    ui->tableWidget_3->blockSignals(false);
    if(dicCode == "11"){
        for(int i=0;i<ui->tableWidget_2->rowCount();i++){
            ReadOnlyDelegate* readOnlyDelegate = new ReadOnlyDelegate(this);
            ui->tableWidget_2->setItemDelegateForColumn(i, readOnlyDelegate);
        }
    }else{
        for(int i=0;i<ui->tableWidget_2->rowCount();i++){
            ui->tableWidget_2->setItemDelegateForColumn(i, nullptr);
        }
    }
    for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
        ui->tableWidget->setRowHeight(i, 50);
    }
    for (int i = 0; i < ui->tableWidget_2->rowCount(); ++i) {
        ui->tableWidget_2->setRowHeight(i, 50);
    }
    for (int i = 0; i < ui->tableWidget_3->rowCount(); ++i) {
        ui->tableWidget_3->setRowHeight(i, 50);
    }

    refComboxInfo();

}

void setup2wind::on_tabWidget_currentChanged(int index)
{
    refTableData();
    if(ui->comboBox->currentText()=="扩增体系(μl)"){
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="DELETE FROM ItemCode WHERE Name=''and DicCode = '4'";
        query.exec(sql);
    }
    refTableData();
}

// 添加
void setup2wind::add_clicked(){
    QString dicCode;
    if(ui->tabWidget->currentIndex()==0){
        if(ui->comboBox->currentText()=="样本名称"){
            dicCode = "1";
        }else if(ui->comboBox->currentText()=="样本类型"){
            dicCode = "2";
        }else if(ui->comboBox->currentText()=="扩增体系(μl)"){
            dicCode = "4";
        }else if(ui->comboBox->currentText()=="耗材类型"){
            dicCode = "6";
        }else if(ui->comboBox->currentText()=="试剂盒"){
            dicCode = "12";
        }

        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql=QString("SELECT Code FROM ItemCode WHERE DicCode='%1'").arg(dicCode);
        QStringList codes;
        query.exec(sql);
        while (query.next()) {
            QString column1Value=query.value("Code").toString();
            codes<<column1Value;
        }
        int code = 0;
        for(int i=1;i<codes.length()+1;i++){
            QString codest = QString::number(i);
            if(!codes.contains(codest)){
                code = codest.toInt();
                break;
            }
        }
        sql=QString("Insert into ItemCode values('%1','%2','','0')").arg(dicCode,QString::number(code));
        query.exec(sql);
        if(dicCode=="7"||dicCode=="8"||dicCode=="9"||dicCode=="10"){
            QSqlDatabase db = dbHelper::openConnection();
            QSqlQuery query(db);
            QString sql=QString("Insert into ItemCode values('%1','%2','','0')").arg("11",QString::number(code));
            query.exec(sql);
        }
        refTableData();
        ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        ui->tableWidget->verticalHeader()->setDefaultSectionSize(40);
        for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
            ui->tableWidget->setRowHeight(i, 50);
        }
    }else if(ui->tabWidget->currentIndex()==1){
        if(ui->comboBox_2->currentText()=="通道一"){
            dicCode = "7";
        }else if(ui->comboBox_2->currentText()=="通道二"){
            dicCode = "8";
        }else if(ui->comboBox_2->currentText()=="通道三"){
            dicCode = "9";
        }else if(ui->comboBox_2->currentText()=="通道四"){
            dicCode = "10";
        }else if(ui->comboBox_2->currentText()=="内标"){
            dicCode = "11";
        }

        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql=QString("Insert into ItemCode values('%1','%2','','0')").arg(dicCode,QString::number(ui->tableWidget_2->rowCount()+1));
        qDebug()<<sql;
        query.exec(sql);
        if(dicCode=="7"||dicCode=="8"||dicCode=="9"||dicCode=="10"){
            QSqlDatabase db = dbHelper::openConnection();
            QSqlQuery query(db);
            QString sql=QString("Insert into ItemCode values('%1','%2','','0')").arg("11",QString::number(ui->tableWidget_2->rowCount()+1));
            query.exec(sql);
        }
        refTableData();
        ui->tableWidget_2->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        ui->tableWidget_2->verticalHeader()->setDefaultSectionSize(40);
        for (int i = 0; i < ui->tableWidget_2->rowCount(); ++i) {
            ui->tableWidget_2->setRowHeight(i, 50);
        }
    }else if(ui->tabWidget->currentIndex()==2){
        if(ui->comboBox_3->currentText()=="基因分析仪"){
            dicCode = "14";
        }else if(ui->comboBox_3->currentText()=="试剂盒类型"){
            dicCode = "15";
        }else if(ui->comboBox_3->currentText()=="体系(μl)"){
            dicCode = "19";
        }else{
            QString info=ui->comboBox_3->currentText();
            QString type=  info.left(info.length()-3);
            QSqlDatabase db = dbHelper::openConnection();
            QSqlQuery query(db);

            QString sql=QString("Select Code From ItemCode Where Name='%1'").arg(type);
            query.exec(sql);
            while (query.next()) {
                dicCode=query.value("Code").toString();
            }
        }

        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        if(dicCode=="15"){
            QString sql=QString("Insert into ItemCode values('%1',"
                                "(Select case "
                                "when  max(DicCode)+1==100 then 101 else  max(DicCode)+1  end as code "
                                "From ItemCode where DicCode<>'100'),'','0')").arg(dicCode);
            query.exec(sql);

            sql=QString("Insert into ItemCode values("
                        "(Select case "
                        "when  max(DicCode)+1==100 then 101 else  max(DicCode)+1  end as code "
                        "From ItemCode where DicCode<>'100'),'','初始试剂盒','0')").arg(dicCode);
            query.exec(sql);
        }else{
            QString sql=QString("Insert into ItemCode values('%1','%2','','0')").arg(dicCode,QString::number(ui->tableWidget_3->rowCount()+1));
            query.exec(sql);
        }

        refTableData();
        ui->tableWidget_3->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        ui->tableWidget_3->verticalHeader()->setDefaultSectionSize(40);
        for (int i = 0; i < ui->tableWidget_3->rowCount(); ++i) {
            ui->tableWidget_3->setRowHeight(i, 50);
        }
    }
}

// 默认
void setup2wind::set_clicked(){

    QString dicCode;
    if(ui->tabWidget->currentIndex()==0){
        if(ui->comboBox->currentText()=="样本名称"){
            dicCode = "1";
        }else if(ui->comboBox->currentText()=="样本类型"){
            dicCode = "2";
        }else if(ui->comboBox->currentText()=="扩增体系(μl)"){
            dicCode = "4";
        }else if(ui->comboBox->currentText()=="耗材类型"){
            dicCode = "6";
        }else if(ui->comboBox->currentText()=="试剂盒"){
            dicCode = "12";
        }
        QPushButton *btn = (QPushButton*)sender();
        QWidget *w_parent = (QWidget*)btn->parent();
        int x = w_parent->frameGeometry().x();
        int y = w_parent->frameGeometry().y();
        QModelIndex index = ui->tableWidget->indexAt(QPoint(x,y));
        QString id = ui->tableWidget->item(index.row(),0)->text();

        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);

        QString sql=QString("SELECT Name FROM ItemCode WHERE DicCode='%1' and IsDefault ='1' ").arg(dicCode);
        QString isname;
        query.exec(sql);
        while (query.next()) {
            QString column2Value = query.value("Name").toString();
            isname.append(column2Value);
        }

        sql=QString("UPDATE ItemCode SET IsDefault =0 WHERE DicCode='%1'").arg(dicCode);
        query.exec(sql);
        if(dicCode == "11"&&id==isname){
            sql=QString("UPDATE ItemCode SET IsDefault ='0' WHERE DicCode='%1' and Code= '%2'").arg(dicCode,id);
            query.exec(sql);
        }else{
            sql=QString("UPDATE ItemCode SET IsDefault ='1' WHERE DicCode='%1' and Code= '%2'").arg(dicCode,id);
            query.exec(sql);
        }
        refTableData();
        ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        ui->tableWidget->verticalHeader()->setDefaultSectionSize(40);
        for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
            ui->tableWidget->setRowHeight(i, 50);
        }
    }else if(ui->tabWidget->currentIndex()==1){
        if(ui->comboBox_2->currentText()=="通道一"){
            dicCode = "7";
        }else if(ui->comboBox_2->currentText()=="通道二"){
            dicCode = "8";
        }else if(ui->comboBox_2->currentText()=="通道三"){
            dicCode = "9";
        }else if(ui->comboBox_2->currentText()=="通道四"){
            dicCode = "10";
        }else if(ui->comboBox_2->currentText()=="内标"){
            dicCode = "11";
        }
        QPushButton *btn = (QPushButton*)sender();
        QWidget *w_parent = (QWidget*)btn->parent();
        int x = w_parent->frameGeometry().x();
        int y = w_parent->frameGeometry().y();
        QModelIndex index = ui->tableWidget_2->indexAt(QPoint(x,y));
        QString id = ui->tableWidget_2->item(index.row(),0)->text();

        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);

        QString sql=QString("SELECT Name FROM ItemCode WHERE DicCode='%1' and IsDefault ='1' ").arg(dicCode);
        QString isname;
        query.exec(sql);
        while (query.next()) {
            QString column2Value = query.value("Name").toString();
            isname.append(column2Value);
        }

        sql=QString("UPDATE ItemCode SET IsDefault =0 WHERE DicCode='%1'").arg(dicCode);
        query.exec(sql);
        if(dicCode == "11"&&id==isname){
            sql=QString("UPDATE ItemCode SET IsDefault ='0' WHERE DicCode='%1' and Code= '%2'").arg(dicCode,id);
            query.exec(sql);
        }else{
            sql=QString("UPDATE ItemCode SET IsDefault ='1' WHERE DicCode='%1' and Code= '%2'").arg(dicCode,id);
            query.exec(sql);
        }
        refTableData();
        ui->tableWidget_2->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        ui->tableWidget_2->verticalHeader()->setDefaultSectionSize(40);
        for (int i = 0; i < ui->tableWidget_2->rowCount(); ++i) {
            ui->tableWidget_2->setRowHeight(i, 50);
        }
    }else if(ui->tabWidget->currentIndex()==2){
        if(ui->comboBox_3->currentText()=="基因分析仪"){
            dicCode = "14";
        }else if(ui->comboBox_3->currentText()=="试剂盒类型"){
            dicCode = "15";
        }else if(ui->comboBox_3->currentText()=="体系(μl)"){
            dicCode = "19";
        }else{
            QString info=ui->comboBox_3->currentText();
            QString type=  info.left(info.length()-3);
            QSqlDatabase db = dbHelper::openConnection();
            QSqlQuery query(db);

            QString sql=QString("Select Code From ItemCode Where Name='%1'").arg(type);
            query.exec(sql);
            while (query.next()) {
                dicCode=query.value("Code").toString();
            }
        }

        QPushButton *btn = (QPushButton*)sender();
        QWidget *w_parent = (QWidget*)btn->parent();
        int x = w_parent->frameGeometry().x();
        int y = w_parent->frameGeometry().y();
        QModelIndex index = ui->tableWidget_3->indexAt(QPoint(x,y));
        QString id = ui->tableWidget_3->item(index.row(),0)->text();

        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);

        QString sql=QString("SELECT Name FROM ItemCode WHERE DicCode='%1' and IsDefault ='1' ").arg(dicCode);
        QString isname;
        query.exec(sql);
        while (query.next()) {
            QString column2Value = query.value("Name").toString();
            isname.append(column2Value);
        }

        sql=QString("UPDATE ItemCode SET IsDefault =0 WHERE DicCode='%1'").arg(dicCode);
        query.exec(sql);
        if(dicCode == "11"&&id==isname){
            sql=QString("UPDATE ItemCode SET IsDefault ='0' WHERE DicCode='%1' and Code= '%2'").arg(dicCode,id);
            query.exec(sql);
        }else{
            sql=QString("UPDATE ItemCode SET IsDefault ='1' WHERE DicCode='%1' and Code= '%2'").arg(dicCode,id);
            query.exec(sql);
        }
        refTableData();
        ui->tableWidget_3->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        ui->tableWidget_3->verticalHeader()->setDefaultSectionSize(40);
        for (int i = 0; i < ui->tableWidget_3->rowCount(); ++i) {
            ui->tableWidget_3->setRowHeight(i, 50);
        }
    }


}

// 删除
void setup2wind::del_clicked(){

    QString dicCode;
    if(ui->tabWidget->currentIndex()==0){
        if(ui->comboBox->currentText()=="样本名称"){
            dicCode = "1";
        }else if(ui->comboBox->currentText()=="样本类型"){
            dicCode = "2";
        }else if(ui->comboBox->currentText()=="扩增体系(μl)"){
            dicCode = "4";
        }else if(ui->comboBox->currentText()=="耗材类型"){
            dicCode = "6";
        }else if(ui->comboBox->currentText()=="试剂盒"){
            dicCode = "12";
        }
        QPushButton *btn = (QPushButton*)sender();
        QWidget *w_parent = (QWidget*)btn->parent();
        int x = w_parent->frameGeometry().x();
        int y = w_parent->frameGeometry().y();
        QModelIndex index = ui->tableWidget->indexAt(QPoint(x,y));
        QString id = ui->tableWidget->item(index.row(),0)->text();

        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);

        QString sql=QString("SELECT IsDefault FROM ItemCode WHERE DicCode=4 and Code='%1'").arg(id);
        query.exec(sql);

        bool isDefault=false;
        while (query.next()) {
            isDefault = query.value("IsDefault").toString() =="1";
        }
        sql=QString("SELECT Name FROM ItemCode WHERE DicCode='%1'").arg(dicCode);
        QStringList sanames;
        query.exec(sql);
        while (query.next()) {
            QString column2Value = query.value("Name").toString();
            sanames.append(column2Value);
        }
        if(sanames.length()==1){
            tcts1 = new tcts();
            tcts1->infochange("提示","info","至少保留一条数据！",false,true);
            // 获取屏幕的中心点
            QRect screenGeometry = QApplication::desktop()->screenGeometry();
            int centerX = screenGeometry.center().x();
            int centerY = screenGeometry.center().y();
            int labelWidth = tcts1->sizeHint().width(); // 获取QLabel的建议大小
            int labelHeight = tcts1->sizeHint().height();
            int labelX = centerX-labelWidth/2;
            int labelY = centerY-labelHeight/2;
            tcts1->show();
            tcts1->move(labelX, labelY);
            return;
        }
        sql=QString("DELETE FROM ItemCode WHERE Code = '%1' and DicCode = '%2'").arg(id,dicCode);
        query.exec(sql);
        if(dicCode=="7"||dicCode=="8"||dicCode=="9"||dicCode=="10"){
            sql=QString("DELETE FROM ItemCode WHERE Code = '%1' and DicCode = '%2'").arg(id,"11");
            query.exec(sql);
        }
        refTableData();
        ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        ui->tableWidget->verticalHeader()->setDefaultSectionSize(40);
        for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
            ui->tableWidget->setRowHeight(i, 50);
        }
    }else if(ui->tabWidget->currentIndex()==1){
        if(ui->comboBox_2->currentText()=="通道一"){
            dicCode = "7";
        }else if(ui->comboBox_2->currentText()=="通道二"){
            dicCode = "8";
        }else if(ui->comboBox_2->currentText()=="通道三"){
            dicCode = "9";
        }else if(ui->comboBox_2->currentText()=="通道四"){
            dicCode = "10";
        }else if(ui->comboBox_2->currentText()=="内标"){
            dicCode = "11";
        }
        QPushButton *btn = (QPushButton*)sender();
        QWidget *w_parent = (QWidget*)btn->parent();
        int x = w_parent->frameGeometry().x();
        int y = w_parent->frameGeometry().y();
        QModelIndex index = ui->tableWidget_2->indexAt(QPoint(x,y));
        QString id = ui->tableWidget_2->item(index.row(),0)->text();

        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);

        QString sql=QString("SELECT IsDefault FROM ItemCode WHERE DicCode=4 and Code='%1'").arg(id);
        query.exec(sql);

        bool isDefault=false;
        while (query.next()) {
            isDefault = query.value("IsDefault").toString() =="1";
        }
        sql=QString("SELECT Name FROM ItemCode WHERE DicCode='%1'").arg(dicCode);
        QStringList sanames;
        query.exec(sql);
        while (query.next()) {
            QString column2Value = query.value("Name").toString();
            sanames.append(column2Value);
        }
        if(sanames.length()==1){
            tcts1 = new tcts();
            tcts1->infochange("提示","info","至少保留一条数据！",false,true);
            // 获取屏幕的中心点
            QRect screenGeometry = QApplication::desktop()->screenGeometry();
            int centerX = screenGeometry.center().x();
            int centerY = screenGeometry.center().y();
            int labelWidth = tcts1->sizeHint().width(); // 获取QLabel的建议大小
            int labelHeight = tcts1->sizeHint().height();
            int labelX = centerX-labelWidth/2;
            int labelY = centerY-labelHeight/2;
            tcts1->show();
            tcts1->move(labelX, labelY);
            return;
        }
        sql=QString("DELETE FROM ItemCode WHERE Code = '%1' and DicCode = '%2'").arg(id,dicCode);
        query.exec(sql);
        if(dicCode=="7"||dicCode=="8"||dicCode=="9"||dicCode=="10"){
            sql=QString("DELETE FROM ItemCode WHERE Code = '%1' and DicCode = '%2'").arg(id,"11");
            query.exec(sql);
        }
        refTableData();
        ui->tableWidget_2->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        ui->tableWidget_2->verticalHeader()->setDefaultSectionSize(40);
        for (int i = 0; i < ui->tableWidget_2->rowCount(); ++i) {
            ui->tableWidget_2->setRowHeight(i, 50);
        }
    }else if(ui->tabWidget->currentIndex()==2){
        if(ui->comboBox_3->currentText()=="基因分析仪"){
            dicCode = "14";
        }else if(ui->comboBox_3->currentText()=="试剂盒类型"){
            dicCode = "15";
        }else if(ui->comboBox_3->currentText()=="体系(μl)"){
            dicCode = "19";
        }else{
            QString info=ui->comboBox_3->currentText();
            QString type=  info.left(info.length()-3);
            QSqlDatabase db = dbHelper::openConnection();
            QSqlQuery query(db);

            QString sql=QString("Select Code From ItemCode Where Name='%1'").arg(type);
            query.exec(sql);
            while (query.next()) {
                dicCode=query.value("Code").toString();
            }
        }
        QPushButton *btn = (QPushButton*)sender();
        QWidget *w_parent = (QWidget*)btn->parent();
        int x = w_parent->frameGeometry().x();
        int y = w_parent->frameGeometry().y();
        QModelIndex index = ui->tableWidget_3->indexAt(QPoint(x,y));
        QString id = ui->tableWidget_3->item(index.row(),0)->text();

        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);

        QString sql=QString("SELECT IsDefault FROM ItemCode WHERE DicCode=4 and Code='%1'").arg(id);
        query.exec(sql);

        bool isDefault=false;
        while (query.next()) {
            isDefault = query.value("IsDefault").toString() =="1";
        }
        sql=QString("SELECT Name FROM ItemCode WHERE DicCode='%1'").arg(dicCode);
        QStringList sanames;
        query.exec(sql);
        while (query.next()) {
            QString column2Value = query.value("Name").toString();
            sanames.append(column2Value);
        }
        if(sanames.length()==1){
            tcts1 = new tcts();
            tcts1->infochange("提示","info","至少保留一条数据！",false,true);
            // 获取屏幕的中心点
            QRect screenGeometry = QApplication::desktop()->screenGeometry();
            int centerX = screenGeometry.center().x();
            int centerY = screenGeometry.center().y();
            int labelWidth = tcts1->sizeHint().width(); // 获取QLabel的建议大小
            int labelHeight = tcts1->sizeHint().height();
            int labelX = centerX-labelWidth/2;
            int labelY = centerY-labelHeight/2;
            tcts1->show();
            tcts1->move(labelX, labelY);
            return;
        }
        sql=QString("DELETE FROM ItemCode WHERE Code = '%1' and DicCode = '%2'").arg(id,dicCode);
        query.exec(sql);
        if(dicCode=="7"||dicCode=="8"||dicCode=="9"||dicCode=="10"){
            sql=QString("DELETE FROM ItemCode WHERE Code = '%1' and DicCode = '%2'").arg(id,"11");
            query.exec(sql);
        }
        refTableData();

        ui->tableWidget_3->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        ui->tableWidget_3->verticalHeader()->setDefaultSectionSize(40);
        for (int i = 0; i < ui->tableWidget_3->rowCount(); ++i) {
            ui->tableWidget_3->setRowHeight(i, 50);
        }
    }
}

void setup2wind::on_tableWidget_cellChanged(int row, int column)
{
    qDebug()<<row<<column;
    QString dicCode;
    if(ui->comboBox->currentText()=="样本名称"){
        dicCode = "1";
    }else if(ui->comboBox->currentText()=="样本类型"){
        dicCode = "2";
    }else if(ui->comboBox->currentText()=="扩增体系(μl)"){
        dicCode = "4";
    }else if(ui->comboBox->currentText()=="耗材类型"){
        dicCode = "6";
    }else if(ui->comboBox->currentText()=="试剂盒"){
        dicCode = "12";
    }else if(ui->comboBox->currentText()=="热盖温度(℃)"){
        dicCode = "20";
    }
//    if(dicCode=="4"){
//        bool isint;
//        ui->tableWidget->item(row,1)->text().toInt(&isint);
//        if(ui->tableWidget->item(row,1)->text().toInt()<=0||!isint){
//            ui->tableWidget->removeRow(row);
//            return;
//        }
//    }


    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString code=ui->tableWidget->item(row,0)->text();
    QString infoname=ui->tableWidget->item(row,column)->text();

    QString sql=QString("Update ItemCode set Name='%1' Where DicCode='%2' and Code='%3'").arg(infoname,dicCode,code);
    query.exec(sql);
    if(dicCode=="7"||dicCode=="8"||dicCode=="9"||dicCode=="10"){
        QString sql=QString("Update ItemCode set Name='%1' Where DicCode='%2' and Code='%3'").arg(infoname,"11",code);
        query.exec(sql);
    }
    refTableData();
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40);
    for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
        ui->tableWidget->setRowHeight(i, 50);
    }
}

void setup2wind::on_tableWidget_2_cellChanged(int row, int column)
{
    QString dicCode;
    if(ui->comboBox_2->currentText()=="通道一"){
        dicCode = "7";
    }else if(ui->comboBox_2->currentText()=="通道二"){
        dicCode = "8";
    }else if(ui->comboBox_2->currentText()=="通道三"){
        dicCode = "9";
    }else if(ui->comboBox_2->currentText()=="通道四"){
        dicCode = "10";
    }else if(ui->comboBox_2->currentText()=="内标"){
        dicCode = "11";
    }
    qDebug()<<dicCode;
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString code=ui->tableWidget_2->item(row,0)->text();
    QString infoname=ui->tableWidget_2->item(row,column)->text();

    QString sql=QString("Update ItemCode set Name='%1' Where DicCode='%2' and Code='%3'").arg(infoname,dicCode,code);
    query.exec(sql);
    if(dicCode=="7"||dicCode=="8"||dicCode=="9"||dicCode=="10"){
        QString sql=QString("Update ItemCode set Name='%1' Where DicCode='%2' and Code='%3'").arg(infoname,"11",code);
        query.exec(sql);
    }
    refTableData();
    ui->tableWidget_2->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget_2->verticalHeader()->setDefaultSectionSize(40);
    for (int i = 0; i < ui->tableWidget_2->rowCount(); ++i) {
        ui->tableWidget_2->setRowHeight(i, 50);
    }
}

void setup2wind::on_tableWidget_3_cellChanged(int row, int column)
{
    QString dicCode;
    if(ui->comboBox_3->currentText()=="基因分析仪"){
        dicCode = "14";
    }else if(ui->comboBox_3->currentText()=="试剂盒类型"){
        dicCode = "15";
    }else if(ui->comboBox_3->currentText()=="体系(μl)"){
        dicCode = "19";
    }else{
        QString info=ui->comboBox_3->currentText();
        QString type=  info.left(info.length()-3);
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);

        QString sql=QString("Select Code From ItemCode Where Name='%1'").arg(type);
        query.exec(sql);
        while (query.next()) {
            dicCode=query.value("Code").toString();
        }
    }
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString code=ui->tableWidget_3->item(row,0)->text();
    QString infoname=ui->tableWidget_3->item(row,column)->text();

    QString sql=QString("Update ItemCode set Name='%1' Where DicCode='%2' and Code='%3'").arg(infoname,dicCode,code);
    query.exec(sql);
    if(dicCode=="7"||dicCode=="8"||dicCode=="9"||dicCode=="10"){
        QString sql=QString("Update ItemCode set Name='%1' Where DicCode='%2' and Code='%3'").arg(infoname,"11",code);
        query.exec(sql);
    }
    refTableData();
    ui->tableWidget_3->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget_3->verticalHeader()->setDefaultSectionSize(40);
    for (int i = 0; i < ui->tableWidget_3->rowCount(); ++i) {
        ui->tableWidget_3->setRowHeight(i, 50);
    }
}

void setup2wind::refComboxInfo(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString currentText=ui->comboBox_3->currentText();
    ui->comboBox_3->blockSignals(true);
    ui->comboBox_3->clear();
    ui->comboBox_3->addItem("基因分析仪");
    ui->comboBox_3->addItem("试剂盒类型");
    ui->comboBox_3->addItem("体系(μl)");

    QString sql="Select Name From ItemCode where DicCode='15'";
    query.exec(sql);

    while (query.next()) {
        QString info=query.value("Name").toString()+"试剂盒";
        ui->comboBox_3->addItem(info);
    }

    ui->comboBox_3->setCurrentText(currentText);
    ui->comboBox_3->blockSignals(false);

}
